package com.app.qtjk.lsh.service;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.app.common.utils.DateFormatUtil;
import com.app.common.utils.FileUploadUtiles;
import com.app.common.utils.ParamConfig;
import com.app.common.utils.ReqJsonUtil;
import com.app.common.utils.StringUtil;
import com.css.sword.kernel.base.annotation.Service;
import com.css.sword.kernel.base.annotation.ServiceContainer;
import com.css.sword.kernel.base.persistence.IPersistenceService;
import com.css.sword.kernel.base.persistence.SQLParam;
import com.css.sword.kernel.utils.SwordLogUtils;
import com.css.sword.kernel.utils.SwordPersistenceUtils;
import com.css.sword.platform.web.event.IReqData;
import com.css.sword.platform.web.event.IResData;
import com.css.sword.platform.web.event.SwordReq;
import com.css.sword.platform.web.event.SwordRes;

@ServiceContainer
public class MessageService {

	private static SwordLogUtils log = SwordLogUtils.getLogger(MessageService.class);

	public MessageService() throws Exception {
		super();
	}


	/**
	 * 得到消息列表
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Service(serviceName = "MessageService_initMessageList")
	public IResData initMessageList(IReqData req) throws Exception {

		

		log.debug("开始进入 MessageService_initMessageList 方法。");

		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();

		//IPageQueryDAO pdao=PageQueryUtils.getPageQueryDAO(); 

		IResData res = new SwordRes();
		HashMap commonMap = new HashMap();
		ArrayList commonList = new ArrayList();
		HashMap hashMap = new HashMap();
		try {
			SQLParam param = new SQLParam();
			param.addParam("yfb");
			param.addParam("admin");
			List<Map<String,Object>> messageMapList = dao.queryMapListByKey("activity_getList", param);
			for (int i = 0; i < messageMapList.size(); i++) {
				Map dataMap=messageMapList.get(i);
				dataMap.put("puntimeStamp", DateFormatUtil.str2timestamp(StringUtil.parse2String(dataMap.get("pubtime")), DateFormatUtil.DATE_FORMAT_SFM));
				Set<String> keySet = dataMap.keySet();
				for (String s : keySet) {
					if (s.equals("hdDjj")) {
						dataMap.put("hdDjj", StringUtil.interceptString(StringUtil.parse2String(dataMap.get(s)), 100, ""));
					}
					if (s.equals("pubtime")) {
						dataMap.put("pubtime", DateFormatUtil.date2str((Date)dataMap.get(s),DateFormatUtil.DATE_FORMAT));
					}
					if (s.equals("hdLogo")) {
						String webfullpath = StringUtil.parse2String(FileUploadUtiles.getFileElemenstMap(StringUtil.parse2String(dataMap.get(s))).get("WebFullPath"));
						dataMap.put("hdLogo", webfullpath);
					}
					if (s.equals("hdWebpath")) {
						dataMap.put("hdWebpath", ParamConfig.getInstanse().getParam("htDomainPath")+StringUtil.parse2String(dataMap.get(s)));
					}
				}
			}
			hashMap.put("returnStatus","true");
			hashMap.put("msg", "成功返回，未出现异常！");
			commonMap.put("commonData", hashMap);
			commonMap.put("messageMapList", messageMapList);
			commonList.add(commonMap);
			res.addListWithDataName("returnData", commonList);
		}catch (Exception e){
			log.debug("---------------系统发生异常！---------------");
			e.printStackTrace();
			hashMap.put("returnStatus","false");
			hashMap.put("msg", "出现异常！");
			commonMap.put("commonData", hashMap);
			commonList.add(commonMap);
			res.addListWithDataName("returnData", commonList);
		}
		return res;

	}

	 		
	/**
	 * 获取消息详情
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Service(serviceName = "MessageService_getMessageDetail")
	public IResData getMessageDetail(IReqData req) throws Exception {

		

		log.debug("开始进入 MessageService_getMessageDetail 方法。");

		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();

		//IPageQueryDAO pdao=PageQueryUtils.getPageQueryDAO(); 

		IResData res = new SwordRes();
		HashMap commonMap = new HashMap();
		ArrayList commonList = new ArrayList();
		HashMap hashMap = new HashMap();
		try {
			SQLParam sqlParam = new SQLParam();
			//获取前台post的数据
			String reqJson = ((SwordReq)req).getReqDataSet().getReqJson();
			Map<String, Object> postMap = ReqJsonUtil.json2map(reqJson);
			String messageId = (String) ((ArrayList)postMap.get("messageId")).get(0);
			
			//String messageId = "941029fd7c1d4cdaac590b2b5ede71ad";
			sqlParam.addParam(messageId);
			
			List<Map<String,Object>> messageDetailMapList = dao.queryMapListByKey("MessageService_getMessageDetail", sqlParam);
			for (int i = 0; i < messageDetailMapList.size(); i++) {
				Map dataMap=messageDetailMapList.get(i);
				Set<String> keySet = dataMap.keySet();
				for (String s : keySet) {
					if (s.equals("mDetailPubdate")) {
						dataMap.put("mDetailPubdate", DateFormatUtil.date2str((Date)dataMap.get(s),DateFormatUtil.DATE_FORMAT));
					}
				}
			}
			hashMap.put("returnStatus","true");
			hashMap.put("msg", "成功返回，未出现异常！");
			commonMap.put("commonData", hashMap);
			commonMap.put("messageDetailMapList", messageDetailMapList);
			commonList.add(commonMap);
			res.addListWithDataName("returnData", commonList);
		}catch (Exception e){
			log.debug("---------------系统发生异常！---------------");
			e.printStackTrace();
			hashMap.put("returnStatus","false");
			hashMap.put("msg", "出现异常！");
			commonMap.put("commonData", hashMap);
			commonList.add(commonMap);
			res.addListWithDataName("returnData", commonList);
		}
		return res;


	}

	 		


}